package com.task.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Item;
import com.entity.Org;
import com.entity.Task;
import com.entity.TaskItem;
import com.entity.Terminal;
import com.interfaces.ItemDao;
import com.interfaces.OrgDao;
import com.interfaces.TaskDao;
import com.interfaces.TaskItemDao;
import com.interfaces.TerminalDao;
import com.task.entity.JsonTask;
import com.utils.JsonResult;
import com.utils.Utils;

/**
 * @Title: TaskService.java
 * @Package com.task.service
 * @author 孙梦楠
 * @date 2017年9月4日 下午1:45:52
 * @version V1.0.0
 */
@Service
public class TaskService {

	@Autowired
	private TaskDao taskdao;

	@Autowired
	private TerminalDao terminaldao;

	@Autowired
	private TaskItemDao taskitemdao;

	@Autowired
	private ItemDao itemdao;
	
	@Autowired
	private OrgDao orgdao;

	// // 列启动未启动状态表单
	// public JsonResult<List<JsonSysdic>> listtaskstatus(String languagecookie) {
	//
	// List<JsonSysdic> jsonSysdics = new ArrayList<JsonSysdic>();
	//
	// if (languagecookie.equals("en")) {
	// JsonSysdic jsonSysdic = new JsonSysdic();
	// jsonSysdic.setSysdicvalue("0");
	// jsonSysdic.setSysdicname("Not Started");
	// jsonSysdics.add(jsonSysdic);
	// jsonSysdic.setSysdicvalue("1");
	// jsonSysdic.setSysdicname("Started");
	// jsonSysdics.add(jsonSysdic);
	// return new JsonResult<List<JsonSysdic>>(601, "任务状态列表", jsonSysdics);
	//
	// } else {
	// JsonSysdic jsonSysdic1 = new JsonSysdic();
	// jsonSysdic1.setSysdicvalue("0");
	// jsonSysdic1.setSysdicname("未启动");
	// jsonSysdics.add(jsonSysdic1);
	// JsonSysdic jsonSysdic2 = new JsonSysdic();
	// jsonSysdic2.setSysdicvalue("1");
	// jsonSysdic2.setSysdicname("启动");
	// jsonSysdics.add(jsonSysdic2);
	//
	// return new JsonResult<List<JsonSysdic>>(601, "任务状态列表", jsonSysdics);
	// }
	//
	// }

	// 列表
	@Transactional
	public JsonResult<List<JsonTask>> listtask(String taskname, String taskstatus, String opeorgcookie,
			String opetypecookie, String languagecookie) {

		List<JsonTask> jsonTasks = new ArrayList<JsonTask>();
		List<Task> tasks = taskdao.findByCondition(taskname, taskstatus, Integer.valueOf(opeorgcookie), opetypecookie);

		for (Task task : tasks) {
			JsonTask jsonTask = dojsontask(task, languagecookie);
			jsonTasks.add(jsonTask);
		}

		return new JsonResult<List<JsonTask>>(602, "列表成功", jsonTasks);
	}

	// 新增
	@Transactional
	public JsonResult<JsonTask> addtask(String taskname, String taskbegin, String taskend, Integer taskperiod,
			String Monday, String Tuesday, String Wednesday, String Thursday, String Friday, String Saturday,
			String Sunday, Integer taskadditionalnum, Integer taskadditionaltime, Integer tasklevel,
			String opeorgcookie, String opetypecookie, String languagecookie, String tasktime,String jobtype) {

		if (Utils.notNullAndEmpty(taskname) && Utils.notNullAndEmpty(taskbegin) && Utils.notNullAndEmpty(taskend)
				&& taskperiod != null && Utils.notNullAndEmpty(Monday) && Utils.notNullAndEmpty(Tuesday)
				&& Utils.notNullAndEmpty(Wednesday) && Utils.notNullAndEmpty(Thursday) && Utils.notNullAndEmpty(Friday)
				&& Utils.notNullAndEmpty(Saturday) && Utils.notNullAndEmpty(Sunday) && Utils.notNullAndEmpty(tasktime)
				&& Utils.notNullAndEmpty(jobtype) && taskadditionalnum != null && taskadditionaltime != null && tasklevel != null
				&& Utils.notNullAndEmpty(opeorgcookie) && Utils.notNullAndEmpty(opetypecookie)) {

			Task task = new Task();
			task.setTaskname(taskname);
			task.setTaskstatus("0");
			task.setTasktype(opetypecookie);
			task.setTaskbegin(taskbegin);
			task.setTaskend(taskend);
			task.setTasktime(tasktime);
			task.setTaskperiod(taskperiod);
			// 星期
			task.setTaskweek(Monday + Tuesday + Wednesday + Thursday + Friday + Saturday + Sunday);
			task.setTaskorg(Integer.valueOf(opeorgcookie));
			task.setTaskadditionalnum(taskadditionalnum);
			task.setTaskadditionaltime(taskadditionaltime);
			task.setTasklevel(tasklevel);
			task.setIsdel((byte) 0);
			// 任务数据
			String cron = getcron();
			task.setCron(cron);
			task.setJobtype(jobtype);

			taskdao.save(task);
			JsonTask jsonTask = dojsontask(task, languagecookie);

			return new JsonResult<JsonTask>(603, "新增成功", jsonTask);
		} else {

			return new JsonResult<JsonTask>(604, "新增失败..传值为空", null);

		}

	}

	// 手动执行任务
	@Transactional
	public JsonResult<Task> startTask(Integer taskid) {
		// 更改任务状态为启用
		if (taskdao.findOne(taskid).getTaskstatus().equals("0")) {
			taskdao.updateTaskstatus(taskid, "1");
		} else if (taskdao.findOne(taskid).getTaskstatus().equals("1")) {
			taskdao.updateTaskstatus(taskid, "0");
		}
		return new JsonResult<Task>(611, "切换任务状态", null);
	}

	// 删除
	@Transactional
	public JsonResult<Task> deleteTask(Integer taskid) {
		if (taskid == null) {
			return new JsonResult<Task>(605, "删除失败..ID为空", null);
		} else {
			taskdao.deleteTask(taskid);

			return new JsonResult<Task>(606, "删除成功", null);
		}
	}

	// 修改界面
	@Transactional
	public JsonResult<JsonTask> updateview(Integer taskid, String languagecookie) {
		if (taskid == null) {
			return new JsonResult<JsonTask>(607, "查找失败..id为空", null);
		} else {
			Task task = taskdao.findOne(taskid);

			return new JsonResult<JsonTask>(608, "查单个任务", dojsontask(task, languagecookie));
		}
	}

	// 修改
	@Transactional
	public JsonResult<JsonTask> updatetask(Integer taskid, String taskname, String taskbegin, String taskend,
			Integer taskperiod, String Monday, String Tuesday, String Wednesday, String Thursday, String Friday,
			String Saturday, String Sunday, Integer taskadditionalnum, Integer taskadditionaltime, Integer tasklevel,
			String opeorgcookie, String opetypecookie, String languagecookie, String tasktime,String jobtype) {

		Task task = new Task();
		task.setTaskid(taskid);
		task.setTaskstatus("0");
		task.setTasktype(opetypecookie);
		task.setTaskorg(Integer.valueOf(opeorgcookie));
		task.setIsdel((byte) 0);
		task.setTaskorg(Integer.valueOf(opeorgcookie));
		task.setTaskweek(Monday + Tuesday + Wednesday + Thursday + Friday + Saturday + Sunday);

		if (Utils.isNull(taskname)) {

			task.setTaskname(taskdao.findOne(taskid).getTaskname());
		} else {
			task.setTaskname(taskname);
		}

		if (Utils.isNull(taskbegin)) {

			task.setTaskbegin(taskdao.findOne(taskid).getTaskbegin());
		} else {
			task.setTaskbegin(taskbegin);
		}

		if (Utils.isNull(taskend)) {

			task.setTaskend(taskdao.findOne(taskid).getTaskend());
		} else {
			task.setTaskend(taskend);
		}

		if (Utils.isNull(tasktime)) {

			task.setTasktime(taskdao.findOne(taskid).getTasktime());
		} else {
			task.setTasktime(tasktime);
		}

		if (taskperiod != null) {

			task.setTaskperiod(taskdao.findOne(taskid).getTaskperiod());
		} else {
			task.setTaskperiod(taskperiod);
		}

		if (taskadditionalnum != null) {

			task.setTaskadditionalnum(taskdao.findOne(taskid).getTaskadditionalnum());
		} else {
			task.setTaskadditionalnum(taskadditionalnum);
		}

		if (taskadditionaltime != null) {

			task.setTaskadditionaltime(taskdao.findOne(taskid).getTaskadditionaltime());
		} else {
			task.setTaskadditionaltime(taskadditionaltime);
		}

		if (tasklevel != null) {

			task.setTasklevel(taskdao.findOne(taskid).getTasklevel());
		} else {
			task.setTasklevel(tasklevel);
		}
		
		if (jobtype != null) {

			task.setJobtype(taskdao.findOne(taskid).getJobtype());
		} else {
			task.setJobtype(jobtype);
		}

		// 任务数据
		String cron = getcron();
		task.setCron(cron);

		taskdao.save(task);
		JsonTask jsonTask = dojsontask(task, languagecookie);

		return new JsonResult<JsonTask>(609, "修改成功", jsonTask);
	}

	// 查询所有任务 0 未启动 ，1 启动 返回任务列表
	@Transactional
	public List<Task> findByTaskstatus(String status) {

		return taskdao.findByTaskstatus(status);
	}

	// 根据任务ID查任务类型和组织架构，继而查所对应的终端
	@Transactional
	public List<Terminal> listTerminalByTaskid(Integer taskid) {
		String type = taskdao.findOne(taskid).getTasktype();
		Integer org = taskdao.findOne(taskid).getTaskorg();
		if (orgdao.findByOrgvalue(org.toString()).getOrgfather().equals("0")) {// 公司
			List<Org> orgs = orgdao.findByOrgfather(org.toString());
			List<Terminal> terminals = new ArrayList<Terminal>();
			for (Org org2 : orgs) {
				List<Terminal> orgterminals = terminaldao
						.findByTerminalorgAndTypeAndTerminalstatusNot(Integer.valueOf(org2.getOrgvalue()), type, "4");
				terminals.addAll(orgterminals);
			}
			return terminals;
		} else { // 小区
			List<Terminal> terminals = terminaldao.findByTerminalorgAndTypeAndTerminalstatusNot(org, type, "4");
			return terminals;
		}
	}

	// 根据任务ID查所关联的Item
	@Transactional
	public List<Item> listItemByTaskid(Integer taskid) {
		List<Item> items = new ArrayList<>();
		List<TaskItem> taskItems = taskitemdao.findByTaskid(taskid);
		for (TaskItem taskItem : taskItems) {
			Item item = new Item();
			item = itemdao.findOne(taskItem.getItemid());
			items.add(item);
		}

		return items;
	}

	// 前端类
	@Transactional
	public JsonTask dojsontask(Task task, String languagecookie) {

		JsonTask jsonTask = new JsonTask();
		jsonTask.setTaskid(task.getTaskid());
		jsonTask.setTaskname(task.getTaskname());

		String taskstatus = dostatus(languagecookie, task.getTaskstatus());
		jsonTask.setTaskstatus(taskstatus);
		jsonTask.setTasktype(task.getTasktype());
		jsonTask.setTaskbegin(task.getTaskbegin());
		jsonTask.setTaskend(task.getTaskend());
		jsonTask.setTasktime(task.getTasktime());
		jsonTask.setTaskperiod(task.getTaskperiod());
		jsonTask.setTaskweek(task.getTaskweek());

		String week = task.getTaskweek();
		int len = week.length();
		char[] arr = new char[len];
		for (int i = 0; i < len; i++) {
			arr[i] = week.charAt(i);
		}
		jsonTask.setMonday(String.valueOf(arr[0]));
		jsonTask.setTuesday(String.valueOf(arr[1]));
		jsonTask.setWednesday(String.valueOf(arr[2]));
		jsonTask.setThursday(String.valueOf(arr[3]));
		jsonTask.setFriday(String.valueOf(arr[4]));
		jsonTask.setSaturday(String.valueOf(arr[5]));
		jsonTask.setSunday(String.valueOf(arr[6]));

		jsonTask.setTaskorg(task.getTaskorg());
		jsonTask.setTaskadditionalnum(task.getTaskadditionalnum());
		jsonTask.setTaskadditionaltime(task.getTaskadditionaltime());
		jsonTask.setTasklevel(task.getTasklevel());

		return jsonTask;
	}

	public String getcron() {

		String cron = null;

		// 函数具体

		return cron;
	}

	// 通过0 1显示不同的界面
	private String dostatus(String languagecookie, String status) {
		String taskstatus = null;
		if (status.equals("1")) {
			if (languagecookie.equals("cn")) {
				taskstatus = "启动";
			} else if (languagecookie.equals("en")) {
				taskstatus = "Started";
			}
		} else if (status.equals("0")) {
			if (languagecookie.equals("cn")) {
				taskstatus = "未启动";
			} else if (languagecookie.equals("en")) {
				taskstatus = "Not Started";
			}
		}
		return taskstatus;
	}

}
